/*
 * VHImageServlet.java
 * Created on 23.09.2005
 *
 * VirtualHockey.net Internet Platform
 * Copyright (c) 2005-2006, Jan Janke
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.server.servlet.base;


import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hockeo.vhbase.data.dao.IGDao;
import com.hockeo.vhbase.data.dao.misc.GImage;

/**
 * <p>
 * This servlet is intended to be used for image retrieval from the database. In order to
 * retrieve an image from the VH database, you must use an HTML pattern similar to the one
 * shown next:
 * </p>
 *
 * <p>
 * <code><img src="VHImageServlet?id=XX" /></code>
 * </p>
 *
 * <p>
 * XX must be replaced by the id of the image you want to retrieve.
 * </p>
 *
 * @version $Id: VHImageServlet.java 48 2006-10-31 20:49:34Z janjanke $
 * @author jankejan
 */
public class VHImageServlet extends HttpServlet
{
  private static Log          s_log          = LogFactory.getLog( VHImageServlet.class );

  private static final String PARAM_IMAGE_ID = "id";

  /*
   * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
   *      javax.servlet.http.HttpServletResponse)
   */
  @Override
  protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
  {
    processRequest( request, response );
  }

  /*
   * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
   *      javax.servlet.http.HttpServletResponse)
   */
  @Override
  protected void doPost( HttpServletRequest request, HttpServletResponse response )
      throws ServletException, IOException
  {
    processRequest( request, response );
  }

  /**
   * Processes the incoming request regardless of the fact whether it is a POST or a GET
   * request.
   *
   * @param request the servlet request object
   * @param response the servlet response object
   * @throws ServletException
   * @throws IOException
   */
  protected void processRequest( HttpServletRequest request, HttpServletResponse response )
      throws ServletException, IOException
  {
    IGDao dao;
    GImage image;
    int nImageId;

    // get the image id
    //
    try
    {
      nImageId = Integer.parseInt( request.getParameter( PARAM_IMAGE_ID ) );
    }
    catch ( NumberFormatException ex )
    {
      s_log.error( "The '" + PARAM_IMAGE_ID + "' parameter has no integer value." );
      throw new ServletException( ex );
    }

    // get the image from the DB
    //
    dao = (IGDao) WebApplicationContextUtils.getWebApplicationContext( getServletContext() ).getBean( "glaceoDao" );

    image = dao.findEntity( GImage.class, nImageId );

    // stream the image back
    //
    if ( image != null )
    {
      response.setContentType( image.getMimeType() );
      response.setContentLength( image.getData().length );
      OutputStream out = response.getOutputStream();
      out.write( image.getData(), 0, image.getData().length );

      // make the image available for garbage collection
      //
      image = null;
    }
  }
}
